<template>
  <div>
    <van-nav-bar
      :title="title1"
      left-text="关闭"
      left-arrow
      @click-left="goback"
    />
    <van-address-edit
      :area-list="areaList"
      show-delete
      :show-set-default="true"
      :show-search-result="true"
      :address-info="addressinfo"
      :search-result="searchResult"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { Toast } from 'vant'
export default {
  name: "addressedit",
  data() {
    return {
      title1: "",
      areaList,
      searchResult: [],
      addressinfo: {},
      user_id: JSON.parse(localStorage.getItem("waimai")).user_id,
      list: {
        id: "",
        name: "",
        tel: "",
        province: "",
        city: "",
        county: "",
        address: "",
        addressDetail: "",
        isDefault: false,
        areaCode: "",
      },
    };
  },
  methods: {
    goback() {
      this.$router.go(-1);
    },
    qingqiu(content, jiekou, miaoshu, id) {
      this.axios({
        method: "post",
        url: "http://localhost:3000/" + jiekou,
        data: {
          content: content,
          user_id: id
          },
      })
        .then((res) => {
          console.log(res);
          Toast.success(miaoshu)
        })
        .catch((err) => {
          console.log(err);
        })
        
    },
    onSave(content) {
      if (this.$route.params.id) {
        this.qingqiu(content, "user_address_edit", "编辑地址成功", this.$route.params.id)
      } else {
        this.qingqiu(content, "user_asset_address", "新增地址成功", this.user_id)
      }

      // this.axios({
      //   method: "post",
      //   url: "http://localhost:3000/user_asset_address",
      //   data: {
      //     content: content,
      //     user_id: this.user_id
      //     },
      // })
      //   .then((res) => {
      //     console.log(res);
      //     Toast.success("新增地址成功")
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });

    },
    onDelete() {
      console.log("删除");
    },
    onChangeDetail(val) {
      console.log(val);
      if (val) {
        this.searchResult = [
          {
            name: "郑州市",
            address: "中原区863软件园",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
  created() {
    if (this.$route.params.id) {
      this.title1 = "编辑收货地址";
      console.log(this.$route.params)
      this.addressinfo = this.$route.params
    } else {
      this.title1 = "新建收货地址";
      this.$nextTick(()=>{
        document.querySelector(".van-button--default").style.display = "none"
      })
    }
  },
};
</script>

<style scoped>
/deep/.van-nav-bar__text,
/deep/.van-icon {
  color: black;
}

/deep/.van-button--danger {
  background-color: rgb(218, 218, 9);
  border: 1px solid rgb(218, 218, 9);
}

/* /deep/.van-button--default {
  display: none;
} */
</style>